package lk.icta.common.web.action;

import lk.icta.common.AppConstants;
import lk.icta.common.dao.DSDivisionDAO;
import lk.icta.common.dao.DistrictDAO;
import lk.icta.common.dao.ProvinceDAO;
import lk.icta.core.dao.LocalAuthorityDAO;
import lk.icta.core.dao.LocalAuthorityTypeDAO;
import lk.icta.core.entity.LocalAuthority;
import lk.icta.core.entity.LocalAuthorityType;
import lk.icta.core.service.LocalAuthorityManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Kusal Samarakoon
 */
public class LocalAuthorityAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(LocalAuthorityAction.class);

    private LocalAuthorityDAO localAuthorityDAO;
    private LocalAuthorityTypeDAO localAuthorityTypeDAO;
    private ProvinceDAO provinceDAO;
    private DistrictDAO districtDAO;
    private DSDivisionDAO dsDivisionDAO;

    private final LocalAuthorityManagementService localAuthorityManagementService;

    private Map<Integer, String> provinceList;
    private Map<Integer, String> districtList;
    private Map<Integer, String> dsDivisionList;
    private Map<Integer, String> localAuthorityTypeList;

    private LocalAuthority localAuthority;
    private List<LocalAuthority> localAuthorityList;

    private int localAuthorityId;
    private int localAuthorityTypeId;
    private int editMode;
    private String districtName;
    private String dsName;


    public LocalAuthorityAction(LocalAuthorityDAO localAuthorityDAO, ProvinceDAO provinceDAO, DistrictDAO districtDAO,
                                DSDivisionDAO dsDivisionDAO, LocalAuthorityTypeDAO localAuthorityTypeDAO,
                                LocalAuthorityManagementService localAuthorityManagementService) {
        this.localAuthorityDAO = localAuthorityDAO;
        this.provinceDAO = provinceDAO;
        this.districtDAO = districtDAO;
        this.dsDivisionDAO = dsDivisionDAO;
        this.localAuthorityTypeDAO = localAuthorityTypeDAO;
        this.localAuthorityManagementService = localAuthorityManagementService;

    }


    public String welcome() {
        logger.debug("Welcome LocalAuthorityAction");
        return SUCCESS;
    }

    public String pageLoad() {
        populateBasicLists();
        return SUCCESS;
    }

    public String addLocalAuthority() {
        logger.debug("addLocalAuthority");
        try {
            logger.debug("LocalAuthority Name {}", localAuthority.getEnName());

            List<LocalAuthority> results = localAuthorityDAO.getLocalAuthorityForDSDivisionByDSDivisionId(user.getDsDivisions().iterator().next().getId(), user);
            if(results!=null && results.size()>0){

                for (LocalAuthority la : results) {
                     if(la.getLocalAuthorityType().getId() == localAuthority.getLocalAuthorityType().getId()){
                         addActionError(getText("fail_duplicate_add_localAuthority.label"));
                         return SUCCESS;
                     }
                }

            }
            localAuthority.setDsDivision(user.getDsDivisions().iterator().next());
            localAuthorityManagementService.addLocalAuthority(localAuthority, user);
            addActionMessage(getText("success_add_localAuthority.label"));
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(getText("fail_add_localAuthority.label"));
            return ERROR;
        }
    }


    public String updateLocalAuthority() {
        logger.debug("updateLocalAuthority");
        try {
             logger.debug("LocalAuthority id {}", localAuthority.getId());
            logger.debug("LocalAuthority Name {}", localAuthority.getEnName());
            logger.debug("LocalAuthority type {}", localAuthority.getLocalAuthorityType().getId());


            //List<LocalAuthority> results = localAuthorityDAO.getLocalAuthorityForDSDivisionByDSDivisionId(user.getDsDivisions().iterator().next().getId(), user);

            /*if(results!=null && results.size()>0){

                for (LocalAuthority la : results) {
                     if(la.getLocalAuthorityType().getId() == localAuthority.getLocalAuthorityType().getId()){
                         addActionError(getText("fail_duplicate_add_localAuthority.label"));
                         return SUCCESS;
                     }
                }

            }*/
            localAuthority.setDsDivision(user.getDsDivisions().iterator().next());
            localAuthorityManagementService.updateLocalAuthority(localAuthority, user);
            addActionMessage(getText("success_edit_localAuthority.label"));
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(getText("fail_edit_localAuthority.label"));
            return ERROR;
        }
    }

    public String deleteLocalAuthority() {
        logger.debug("deleteLocalAuthority");
        try {
            logger.debug("LocalAuthority id {}", localAuthorityId);

            localAuthorityManagementService.inActiveLocalAuthority(localAuthorityId,user);
            addActionMessage(getText("success_delete_localAuthority.label"));
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(getText("fail_delete_localAuthority.label"));
            return ERROR;
        }
    }

    public String getLocalAuthorityListPageLoad(){
        logger.debug("getLocalAuthorityListPageLoad");
        int dsId=user.getDsDivisions().iterator().next().getId();
        localAuthorityList=localAuthorityDAO.getLocalAuthorityForDSDivisionByDSDivisionId(dsId,user);
        return SUCCESS;
    }

    public String loadLocalAuthorityForm(){
      logger.debug("loadLocalAuthorityForm localAuthorityId {}",localAuthorityId);
        localAuthority=localAuthorityDAO.getLocalAuthorityById(localAuthorityId,user);
        editMode=1;
        populateBasicLists();
        return SUCCESS;
    }

    private void populateBasicLists() {
        /*provinceList = provinceDAO.getProvinceList();
        districtList = districtDAO.getAllDistrictNamesByProvinceId(1, language, user);
        dsDivisionList = dsDivisionDAO.getDSDivisionNamesByDistrictId(1, language, user);*/

        dsName=user.getDsDivisions().iterator().next().getEnDivisionName();
        districtName=user.getDsDivisions().iterator().next().getDistrict().getEnDistrictName();

        //get localAuthority Types
        getLocalAuthorityTypes();

    }

    private void getLocalAuthorityTypes() {
        List<LocalAuthorityType> results = localAuthorityTypeDAO.getAllLocalAuthorities();
        logger.debug("Auth type list {}", results.size());
        localAuthorityTypeList = new HashMap<Integer, String>();
        for (LocalAuthorityType la : results) {
            if (AppConstants.ENGLISH.equals(language)) {
                localAuthorityTypeList.put(la.getId(), la.getEnName());

            } else if (AppConstants.SINHALA.equals(language)) {
                localAuthorityTypeList.put(la.getId(), la.getSiName());

            } else if (AppConstants.TAMIL.equals(language)) {
                localAuthorityTypeList.put(la.getId(), la.getTaName());

            }
        }
    }


    public Map<Integer, String> getProvinceList() {
        return provinceList;
    }

    public void setProvinceList(Map<Integer, String> provinceList) {
        this.provinceList = provinceList;
    }

    public Map<Integer, String> getDistrictList() {
        return districtList;
    }

    public void setDistrictList(Map<Integer, String> districtList) {
        this.districtList = districtList;
    }

    public Map<Integer, String> getDsDivisionList() {
        return dsDivisionList;
    }

    public void setDsDivisionList(Map<Integer, String> dsDivisionList) {
        this.dsDivisionList = dsDivisionList;
    }

    public Map<Integer, String> getLocalAuthorityTypeList() {
        return localAuthorityTypeList;
    }

    public void setLocalAuthorityTypeList(Map<Integer, String> localAuthorityTypeList) {
        this.localAuthorityTypeList = localAuthorityTypeList;
    }

    public LocalAuthority getLocalAuthority() {
        return localAuthority;
    }

    public void setLocalAuthority(LocalAuthority localAuthority) {
        this.localAuthority = localAuthority;
    }

    public List<LocalAuthority> getLocalAuthorityList() {
        return localAuthorityList;
    }

    public void setLocalAuthorityList(List<LocalAuthority> localAuthorityList) {
        this.localAuthorityList = localAuthorityList;
    }

    public int getLocalAuthorityId() {
        return localAuthorityId;
    }

    public void setLocalAuthorityId(int localAuthorityId) {
        this.localAuthorityId = localAuthorityId;
    }

    public int getEditMode() {
        return editMode;
    }

    public void setEditMode(int editMode) {
        this.editMode = editMode;
    }

    public String getDistrictName() {
        return districtName;
    }

    public void setDistrictName(String districtName) {
        this.districtName = districtName;
    }

    public String getDsName() {
        return dsName;
    }

    public void setDsName(String dsName) {
        this.dsName = dsName;
    }

    public int getLocalAuthorityTypeId() {
        return localAuthorityTypeId;
    }

    public void setLocalAuthorityTypeId(int localAuthorityTypeId) {
        this.localAuthorityTypeId = localAuthorityTypeId;
    }
}
